home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 5674 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.5 KB  |  43 lines

  1. Newsgroups: comp.lang.c,comp.graphics.algorithms,rec.games.programmer
  2. Path: mxsld2.pd.infn.it!LORETI
  3. From: loreti@mxsld2.pd.infn.it (Maurizio Loreti)
  4. Subject: Re: Speed question here...
  5. X-Nntp-Posting-Host: mxsld2.pd.infn.it
  6. Message-ID: <Dn2nAL.Jn4@news.cern.ch>
  7. Sender: news@news.cern.ch (USENET News System)
  8. Reply-To: loreti@mxsld2.pd.infn.it
  9. Organization: I.N.F.N. Padova - CDF/CMS VAXcluster
  10. References: <4ftluh$1gkv@hearst.cac.psu.edu>,<4gc5ce$rtv@news.eunet.ch>
  11. Date: Tue, 20 Feb 1996 11:15:55 GMT
  12.  
  13. In article <4gc5ce$rtv@news.eunet.ch>, Reto Koradi <kor@spectrospin.ch> writes:
  14. >koscho@wjk130.rh.psu.edu (William Koscho) wrote:
  15. >>I was curious as to how fast something like the 
  16. >>following would execute:
  17. >>
  18. >>    int x;
  19. >>    node *ptr;   ptr in linked list
  20. >>
  21. >>       for ( ptr = first_node; ptr != NULL; ptr = ptr.next ) {
  22. >>       for ( x = 0; x < max; x++ ) {
  23. >>        array[x] = array_other[x];
  24. >>           }
  25. >>      } 
  26. >
  27. >You could take the inner loop outside of the outer loop, it doesn't depend
  28. >on ptr. A good compiler might do that automatically, but it certainly
  29. >couldn't hurt. So replace it by:
  30. >
  31. >    for ( x = 0; x < max; x++ ) {
  32. >      array[x] = array_other[x];
  33. >    }
  34. >    for ( ptr = first_node; ptr != NULL; ptr = ptr.next ) {
  35. >    }
  36.  
  37. Then optimize the last loop to ptr = NULL; you see, traversing the
  38. tree costs nothing at all.
  39. 8^)
  40. --
  41. Maurizio Loreti                       http://mvxpd5.pd.infn.it/wwwcdf/mlo.html
  42. Un. of Padova, Dept. of Physics - Padova, Italy          loreti@padova.infn.it
  43.